package cn.iocoder.yudao.module.cmt.controller.admin.activity;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.activity.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.activity.ActivityCategoryDO;
import cn.iocoder.yudao.module.cmt.service.activity.ActivityCategoryService;

@Tag(name = "管理后台 - 活动分类")
@RestController
@RequestMapping("/cmt/activity-category")
@Validated
public class ActivityCategoryController {

    @Resource
    private ActivityCategoryService activityCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建活动分类")
    @PreAuthorize("@ss.hasPermission('cmt:activity-category:create')")
    public CommonResult<Long> createActivityCategory(@Valid @RequestBody ActivityCategorySaveReqVO createReqVO) {
        return success(activityCategoryService.createActivityCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动分类")
    @PreAuthorize("@ss.hasPermission('cmt:activity-category:update')")
    public CommonResult<Boolean> updateActivityCategory(@Valid @RequestBody ActivityCategorySaveReqVO updateReqVO) {
        activityCategoryService.updateActivityCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:activity-category:delete')")
    public CommonResult<Boolean> deleteActivityCategory(@RequestParam("id") Long id) {
        activityCategoryService.deleteActivityCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除活动分类")
    @PreAuthorize("@ss.hasPermission('cmt:activity-category:delete')")
    public CommonResult<Boolean> deleteActivityCategoryList(@RequestParam("ids") List<Long> ids) {
        activityCategoryService.deleteActivityCategoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:activity-category:query')")
    public CommonResult<ActivityCategoryRespVO> getActivityCategory(@RequestParam("id") Long id) {
        ActivityCategoryDO activityCategory = activityCategoryService.getActivityCategory(id);
        return success(BeanUtils.toBean(activityCategory, ActivityCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动分类分页")
    @PreAuthorize("@ss.hasPermission('cmt:activity-category:query')")
    public CommonResult<PageResult<ActivityCategoryRespVO>> getActivityCategoryPage(@Valid ActivityCategoryPageReqVO pageReqVO) {
        PageResult<ActivityCategoryDO> pageResult = activityCategoryService.getActivityCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动分类 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:activity-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportActivityCategoryExcel(@Valid ActivityCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityCategoryDO> list = activityCategoryService.getActivityCategoryPage(pageReqVO).getList();
        ExcelUtils.write(response, "活动分类.xls", "数据", ActivityCategoryRespVO.class,
                        BeanUtils.toBean(list, ActivityCategoryRespVO.class));
    }

}
